<?php

namespace app\admin\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class Tenant extends Controller
{
    public function tenantlist()
    {
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_info")->order("create_time desc")->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_info')->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    public function tenantlistes()
    {
        $list = DB::name("tenant_info")->order("create_time desc")->select();
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $list]));
    }
    public function profitList()
    {
        $remark  = trim(input('param.remark'));
        $dealType  = trim(input('param.dealType'));
        $tenantId  = trim(input('param.tenantId'));
        $where=array(
            "deal_type" => $dealType,
            "tenant_id" => $tenantId,
            "remark" => $remark
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_profit")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_profit')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    public function flowList()
    {
        $remark  = trim(input('param.remark'));
        $type  = trim(input('param.type'));
        $tenantId  = trim(input('param.tenantId'));
        $where=array(
            "type" => $type,
            "tenant_id" => $tenantId,
            "remark" => $remark
        );
        foreach ($where as $key=>$value)
        {
            if(empty($value)){
                unset($where[$key]);
            }
        }
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_flow")->order("create_time desc")->where($where)->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_flow')->where($where)->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
    }
    public function switchs()
    {
        $id = trim(input('param.tenantId'));
        $status = trim(input('param.status'));
        $this->db = new SysDb();
        $data = $this->db->table('tenant_info')->where(['id' => $id])->item();
        $data['status']=$status;
        $this->db->table('tenant_info')->where(['id' => $id])->update($data);
        
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
    }

     public function getTenantExpressCostList(){
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list = Db::table('sys_express_sheet')
             ->alias('ses')
             ->join('sys_express_fee sef', 'sef.express_sheet_id = ses.id',"left")
             ->limit($tol, $limit)
            //  ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.other_type'=>'other_type','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
             ->select();
        $count = Db::table('sys_express_sheet')
        ->alias('ses')
        ->join('sys_express_fee sef', 'sef.express_sheet_id = ses.id',"left")
       
        // ->field(array(' gi.store_id' =>'storeId','gi.types'=>'types','gi.other_type'=>'other_type','gi.id'=>"giId",'gi.status'=>"giStatus",'gi.cover','gi.name','gi.sales_num'=>'salesNum','gi.descr','gi.weight','gi.cost','tgp.id'=>'id','tgp.profit'=>'profit','gi.repertory'=>"repertory"))
             ->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
     }
     public function setExpressCost(){
        $id = trim(input('param.id'));
        $cost = trim(input('param.cost'));
        $this->db = new SysDb();
        $data = $this->db->table('sys_express_fee')->where(['id' => $id])->item();
        $data['tenant_cost']=$cost;
        $this->db->table('sys_express_fee')->where(['id' => $id])->update($data);
        
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
     }
     public function getTenantGoodsCostList(){
         $id=trim(input('param.tenantId'));
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $tol = (intval($page) - 1) * $limit;
        $list =  Db::table('goods_info')
        ->alias(' gi')
        ->join('goods_store gs', 'gi.store_id = gs.id',"left")
        ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
        ->where(['tenant_id' => $id])
        ->limit($tol, $limit)
        ->select();
        $count = Db::table('goods_info')
        ->alias(' gi')
        ->join('goods_store gs', 'gi.store_id = gs.id',"left")
        ->join('tenant_goods_price tgp', 'gi.id = tgp.goods_id',"left")
        ->where(['tenant_id' => $id])
        ->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
     }
     public function setGoodsCost(){
        $id = trim(input('param.id'));
        $cost = trim(input('param.cost'));
        $this->db = new SysDb();
        $data = $this->db->table('goods_info')->where(['id' => $id])->item();
        $data['tenant_cost']=$cost;
        $this->db->table('goods_info')->where(['id' => $id])->update($data);
        
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
     }
     public function accountList(){
        $page = trim(input('param.pageNo'));
        $limit = trim(input('param.pageSize'));
        $id= trim(input('param.tenantId'));
        $tol = (intval($page) - 1) * $limit;
        $list = DB::name("tenant_account")->order("create_time desc")->where(['tenant_id' => $id])->limit($tol, $limit)->select();
        $this->db = new SysDb();
        $count = $this->db->table('tenant_account')->where(['tenant_id' => $id])->count();
        $data = array();
        array_push($data, $list);
        array_push($data, $count);
        exit(json_encode(['code' => 1, 'message' => 'succ', "result" => $data]));
     }
     public function updatePassword(){
        $id = trim(input('param.id'));
        $password = trim(input('param.password'));
        $this->db = new SysDb();
        $count = $this->db->table('tenant_account')->where(['tenant_id' => $id])->item();
        $count['password']=md5($password);
        $a=$this->db->table('tenant_account')->where(['id' => $id])->update($count);
        if($a){
            exit(json_encode(['code' => 1, 'message' => 'succ', "result" => "操作成功"]));
        }
     }

        public function update()
        {
            $qq = trim(input('param.qq'));
            $keywords = trim(input('param.keywords'));
            $favicon = trim(input('param.favicon'));
            $totalBalance = trim(input('param.total_balance'));
            $wechat = trim(input('param.wechat'));
            $title = trim(input('param.title'));
            $descr = trim(input('param.descr'));
            $balance = trim(input('param.balance'));
            $rechargeAmount=trim(input('param.recharge_amount'));
            $phone = trim(input('param.phone'));
            $domainName = trim(input('param.domain_name'));
            $contactNumber = trim(input('param.contact_number'));
            $logo = trim(input('param.logo'));
            $company = trim(input('param.company'));
            $heads = trim(input('param.heads'));
            $id = trim(input('param.id'));
            $spreadAmount = trim(input('param.spread_amount'));
            $status = trim(input('param.status'));
            $array=array();
            array_push( $array,$favicon);
            array_push( $array, $wechat);
            array_push( $array, $logo);
            array_push( $array,$heads);
            foreach ($array as  $k=>&$value) {
                $needle = 'base64'; //判断是否包含a这个字符
                $tmparray = explode($needle, $value);
                if (count($tmparray) > 1) {
                    $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
                    if (strstr($value, ",")) {
                        $image = explode(',', $value);
                        $image = $image[1];
                    }
                    $path = "./uploads/" . date("Ymd", time());
                    $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
                    if (!is_dir($path)) { //判断目录是否存在 不存在就创建
                        mkdir($path, 0777, true);
                    }
                    $imageSrc = $path . "/" . $imageName; //图片名字
                    $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
                    $imageSrcs = $paths . "/" . $imageName; //图片名字
                    $imageSrcs = $paths . "/" . $imageName; //图片名字
                    if ($r) {
                        $value=$imageSrcs;
                    }
                }
            }
                $this->db = new SysDb();
                $admin = $this->db->table('tenant_info')->where(['id ' => $id])->item();
                $admin['qq'] = $qq;
                $admin['keywords'] = $keywords;
                $admin['favicon'] = $array[0];
                $admin['total_balance'] = $totalBalance;
                $admin['wechat'] = $array[1];
                $admin['title'] = $title;
                $admin['descr'] = $descr;
                $admin['balance'] = $balance;
                $admin['recharge_amount'] = $rechargeAmount;
                $admin['phone'] = $phone;
                $admin['domain_name'] = $domainName;
                $admin['contact_number'] = $contactNumber;
                $admin['logo'] = $array[2];
                $admin['company'] = $company;
                $admin['heads'] = $array[3];
                $admin['id'] = $id;
                $admin['spread_amount'] = $spreadAmount;
                $admin['status'] = $status;
                $admins = $this->db->table('tenant_info')->where(['id ' => $id])->update($admin);
                if ($admins) {
                    exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
                }
        }
        public function add(){
            $qq = trim(input('param.qq'));
            $keywords = trim(input('param.keywords'));
            $favicon = trim(input('param.types'));
            $totalBalance = trim(input('param.total_balance'));
            $wechat = trim(input('param.wechat'));
            $title = trim(input('param.title'));
            $descr = trim(input('param.descr'));
            $balance = trim(input('param.balance'));
            $rechargeAmount=trim(input('param.recharge_amount'));
            $phone = trim(input('param.phone'));
            $domainName = trim(input('param.domain_name'));
            $contactNumber = trim(input('param.contact_number'));
            $logo = trim(input('param.logo'));
            $company = trim(input('param.company'));
            $heads = trim(input('param.heads'));
            $spreadAmount = trim(input('param.spread_amount'));
            $status = trim(input('param.status'));
            $array=array();
            array_push( $array,$favicon);
            array_push( $array, $wechat);
            array_push( $array, $logo);
            array_push( $array,$heads);
            foreach ($array as  $k=>&$value) {
                $needle = 'base64'; //判断是否包含a这个字符
                $tmparray = explode($needle, $value);
                if (count($tmparray) > 1) {
                    $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . '.png';
                    if (strstr($value, ",")) {
                        $image = explode(',', $value);
                        $image = $image[1];
                    }
                    $path = "./uploads/" . date("Ymd", time());
                    $paths = "http://127.0.0.1:8000/uploads/" . date("Ymd", time());
                    if (!is_dir($path)) { //判断目录是否存在 不存在就创建
                        mkdir($path, 0777, true);
                    }
                    $imageSrc = $path . "/" . $imageName; //图片名字
                    $r = file_put_contents($imageSrc, base64_decode($image)); //返回的是字节数
                    $imageSrcs = $paths . "/" . $imageName; //图片名字
                    $imageSrcs = $paths . "/" . $imageName; //图片名字
                    // if ($r) {
                        $value=$imageSrcs;
                    // }
                }
            }
                $this->db = new SysDb();
                $admin['qq'] = $qq;
                $admin['keywords'] = $keywords;
                $admin['favicon'] = $array[0];
                $admin['total_balance'] = $totalBalance;
                $admin['wechat'] = $array[1];
                $admin['title'] = $title;
                $admin['descr'] = $descr;
                $admin['balance'] = $balance;
                $admin['recharge_amount'] = $rechargeAmount;
                $admin['phone'] = $phone;
                $admin['domain_name'] = $domainName;
                $admin['contact_number'] = $contactNumber;
                $admin['logo'] = $array[2];
                $admin['company'] = $company;
                $admin['heads'] = $array[3];
                $admin['spread_amount'] = $spreadAmount;
                $admin['status'] = $status;
                $admins = $this->db->table('tenant_info')->insert($admin);
                if ($admins) {
                    exit(json_encode(['message' => 'succ', 'result' => "修改成功"]));
                }
        }
     
     
}
